<template>
  <div class="container">
        <!-- 搜索框 -->
    <div class="search-box">
      <el-input
        v-model="searchKeyword"
        :placeholder="getPlaceholder"
        class="search-input"
        clearable
      >
      </el-input>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

    <!-- 众筹列表 -->
  <el-row :gutter="20">
    <el-col :span="8" v-for="item in pageList" :key="item.id">
      <el-card class="project-card" shadow="hover" @click="goToCorwdfuningDetail(item.projectId)">
        <div class="project-content">
          <div class="status-tag" :class="getStatusClass(item.status)">
            {{ getStatusText(item.status) }}
          </div>
          <div class="project-img">
            <el-image 
              :src="item.image" 
              fit="cover"
              :alt="item.title"
            />
          </div>
          <div class="project-info">
            <h3 class="title" :title="item.title">{{ item.projectName }}</h3>
            <div class="info-item">
              <i class="el-icon-user"></i>
              <span>发起人: {{ item.initiator }}</span>
            </div>
            <div class="info-item">
              <i class="el-icon-goods"></i>
              <span>目标数量: {{ item.targetQuantity }}</span>
            </div>
            <div class="info-item">
              <i class="el-icon-user-solid"></i>
              <span>已参与人数: {{ item.currtentQuantity }}</span>
            </div>
            <div class="project-progress">
              <div class="progress-text">
                <span>众筹进度</span>
                <span>{{ ((item.currentQuantity / item.targetQuantity) * 100).toFixed(1)  }}%</span>
              </div>
              <el-progress 
                :percentage="((item.currentQuantity / item.targetQuantity) * 100)"
                :stroke-width="8"
                :status="getProgressStatus(item.status)"
                :color="getProgressColor(item.status)"
              />
            </div>
            <div class="project-time">
              <i class="el-icon-time"></i>
              <span>{{ item.startTime }} 至 {{ item.endTime }}</span>
            </div>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
  
            <!--分页区域-->
      <el-pagination
        class="pagination"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[6]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="sizes,prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="position: absolute ;bottom: 30px; margin-top: 30px; margin-left: 20%;"
        />

  </div>
</template>

<script setup>
import { ref,onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { searchCorwdfuningApi } from '@/api/search'

// 搜索关键词
const searchKeyword = ref('')

const crowdfundingList = ref([])
// const crowdfundingList = ref([
//     {
//     id:1,
//     image: 'https://cbu01.alicdn.com/img/ibank/2019/830/974/10449479038_998666923.jpg',
//     title: '可持续时尚:环保T恤系列',
//     initiator:'张三',
//     targetQuantity: 500,
//     currtentQuantity: 200,
//     startime: '2023-03-01',
//     endtime: '2023-05-10',
//     status: 'funding'
//   },
//     {
//     id:2,
//     image: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.k4VWpMVQWgzNJ-2aMPDY1QHaHa?pid=ImgDet&w=474&h=474&rs=1',
//     title: '舒适亲肤，防晒吸汗T恤',
//     initiator:'张三',
//     targetQuantity: 300,
//     currtentQuantity: 600,
//     startime: '2023-03-01',
//     endtime: '2023-05-10',
//     status: 'success',
//     },
//     {
//     id:1,
//     image: 'https://cbu01.alicdn.com/img/ibank/2019/830/974/10449479038_998666923.jpg',
//     title: '可持续时尚:环保T恤系列',
//     initiator:'张三',
//     targetQuantity: 500,
//     currtentQuantity: 600,
//     startime: '2023-03-01',
//     endtime: '2023-05-10',
//     status: 'completed',

//   },
//       {
//     id:2,
//     image: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.k4VWpMVQWgzNJ-2aMPDY1QHaHa?pid=ImgDet&w=474&h=474&rs=1',
//     title: '舒适亲肤，防晒吸汗T恤',
//     targetQuantity: 300,
//     initiator:'张三',
//     currtentQuantity: 150,
//     startime: '2023-03-01',
//     endtime: '2023-05-10',
//     status: 'failed',

//   }
// ]);



// 搜索处理

const pageList = ref([]); // 当前页的数据（用于显示）


const getCrowdFundingList = async () => {
  const res = await searchCorwdfuningApi('')
  crowdfundingList.value = res.data
  console.log(crowdfundingList.value)
  total.value = crowdfundingList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}

// 确保在组件挂载时调用
onMounted(() => {
  getCrowdFundingList()
})

// 搜索处理
const handleSearch = async () => {
  const res = await searchCorwdfuningApi(searchKeyword.value)
  crowdfundingList.value = res.data
  total.value = crowdfundingList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}

//分页
const total = ref(0); // 总条数
const pagePages = ref(1); // 总页数
const currentPage = ref(1); // 当前页码
const pageSize = ref(6);  // 每页显示条数

// 更新分页数据（根据 currentPage 和 pageSize）
const updatePaginatedList = () => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  pageList.value = crowdfundingList.value.slice(start, end);  // 切割出当前页的数据
};

// 处理每页条数变化
const handleSizeChange = (newSize) => {
  pageSize.value = newSize; // 更新每页显示的条目数
  updatePaginatedList();  // 更新分页数据
};

// 处理当前页码变化
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage; // 更新当前页码
  updatePaginatedList();  // 更新分页数据
};


// 跳转到详情页方法
const router = useRouter()
// 跳转工厂详情
const goToCorwdfuningDetail = (id) => {
  const routeData = router.resolve({
    name: 'crowdFundingDetail',
    params: { projectid: id }
  })
  // 在新窗口打开
  window.open(routeData.href, '_blank')
}

// 状态文本映射
const statusMap = {
  'funding': '进行中',
  'success': '已成功',
  'completed': '已结束',
  'failed': '已失败'
}

// 获取状态文本
const getStatusText = (status) => {
  return statusMap[status] || status
}

// 获取状态对应的类名
const getStatusClass = (status) => {
  return `status-${status}`
}

// 获取进度条状态
const getProgressStatus = (status) => {
  if (status === 'failed') return 'failed'
  if (status === 'funding') return 'funding'
  if (status === 'success') return 'success'
  if (status === 'completed') return 'completed'
  return ''
}

// 获取进度条颜色
const getProgressColor = (status) => {
  const colors = {
    'funding': '#FA5566',
    'success': '#67C23A',
    'completed': '#909399',
    'failed': '#F56C6C'
  }
  return colors[status] || '#FA5566'
}
</script>

<style scoped lang="scss">
.container{
  min-height: 100vh;
}


.search-box {
  max-width: 600px;
  margin: 0 auto 20px;
  display: flex;
  .search-input {
    width: 100%;
  }
  .el-button {
    margin-left: 20px;
    width: 80px;
  }
}

.project-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative; // 为状态标签定位

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
  }
}

// 状态标签样式
.status-tag {
  position: absolute;
  top: 16px;
  right: 16px;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  color: #fff;
  z-index: 1;
  
  // 不同状态的颜色
  &.status-funding {
    background-color: #FA5566;
  }
  
  &.status-success {
    background-color: #67C23A;
  }
  
  &.status-completed {  
    background-color: #909399;  
  }
  
  
  &.status-failed {
    background-color: #F56C6C;
  }
}

.project-content {
  display: flex;
  flex-direction: column;
}

.project-img {
  width: 100%;
  height: 200px;
  overflow: hidden;
  border-radius: 8px;
  margin-bottom: 16px;
  
  .el-image {
    width: 100%;
    height: 100%;
    transition: transform 0.3s ease;
    
    &:hover {
      transform: scale(1.05);
    }
  }
}

.project-info {
  padding: 0 12px;
  
  .title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0 0 16px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  .info-item {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    color: #666;
    font-size: 14px;
    
    i {
      margin-right: 8px;
      font-size: 16px;
      color: #999;
    }
  }
  
  .project-progress {
    margin: 16px 0;
    
    .progress-text {
      display: flex;
      justify-content: space-between;
      margin-bottom: 8px;
      color: #666;
      font-size: 14px;
    }
    
    .el-progress {
      margin-top: 8px;
    }

    // 根据状态设置进度条颜色
    :deep(.el-progress-bar__inner) {
      transition: all 0.3s ease;
    }
    
    :deep(.el-progress.is-success .el-progress-bar__inner) {
      background-color: #67C23A;
    }
    
    :deep(.el-progress.is-exception .el-progress-bar__inner) {
      background-color: #F56C6C;
    }
  }
  
  .project-time {
    display: flex;
    align-items: center;
    color: #999;
    font-size: 13px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #f0f0f0;
    
    i {
      margin-right: 8px;
    }
  }
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .el-col {
    width: 25%;
  }
}

@media screen and (max-width: 992px) {
  .el-col {
    width: 33.33%;
  }
}

@media screen and (max-width: 768px) {
  .el-col {
    width: 50%;
  }
}

@media screen and (max-width: 576px) {
  .el-col {
    width: 100%;
  }
}
</style>